#if 0
#include "mdata.h"
#include "nvmc.h"

#define MDATA_COUNTER_FLAG     0xA1A2A3A4
#define MDATA_COUNTER_SIZE     4

MDevStat_e mDataCounterInit(MDataCounter_t *counter, uint32_t start0,
                            uint32_t start1, uint32_t end)
{
	const uint32_t *addr;
	uint32_t count;
	uint32_t tmp1, i;

	count = 0;
	counter->count0Start = start0;
	counter->count1Start = start1;
	counter->countEnd = end;
	tmp1 = *(uint32_t *)counter->count0Start;

	if(tmp1 != MDATA_COUNTER_FLAG)
	{
		return mDataCounterClear(counter);
	}

	for(addr = (uint32_t *)(counter->count0Start+MDATA_COUNTER_SIZE);
	    addr < (uint32_t *)counter->count1Start; addr++)
	{
		tmp1 = *addr;

		if(tmp1 == 0x00000000)
		{
			count += 32;
		}
		else
		{
			for(i = 0; i < 32; i++)
			{
				if(tmp1 & 0x1)
				{
					break;
				}

				count++;
				tmp1 >>= 1;
			}

			break;
		}
	}

	counter->count0Addr = addr;
	counter->count = count * (counter->countEnd-counter->count1Start)*8;
	count = 0;

	for(addr = (uint32_t *)counter->count1Start;
	    addr < (uint32_t *)counter->countEnd; addr++)
	{
		tmp1 = *addr;

		if(tmp1 == 0x00000000)
		{
			count += 32;
		}
		else
		{
			for(i = 0; i < 32; i++)
			{
				if(tmp1 & 0x1)
				{
					break;
				}

				count++;
				tmp1 >>= 1;
			}

			break;
		}
	}

	counter->count1Addr = addr;
	counter->count += count;

	return MDEV_STAT_OK;
}

MDevStat_e mDataCounterClear(MDataCounter_t *counter)
{
	NVMC_EraseSector((uint32_t *)counter->count0Start,
	                 (counter->countEnd-counter->count0Start)
	                 /SOC_FLASH_SECTOR_SIZE);
	NVMC_ProgramWord((uint32_t *)counter->count0Start, MDATA_COUNTER_FLAG);
	counter->count0Addr = (const uint32_t *)counter->count0Start
	                      +MDATA_COUNTER_SIZE/4;
	counter->count1Addr = (const uint32_t *)counter->count1Start;
	counter->count = 0;

	return MDEV_STAT_OK;
}

uint32_t mDataCounterGetValue(const MDataCounter_t *counter)
{
	return counter->count;
}

MDevStat_e mDataCounterSetValue(MDataCounter_t *counter, uint32_t value)
{
	uint32_t tmp0, tmp1, tmp2, tmp3;
	const uint32_t *count0Addr;
	const uint32_t *count1Addr;
	uint32_t value0, value1;
	uint32_t *addr;

	if(value == 0)
	{
		mDataCounterClear(counter);
	}

	tmp2 = (counter->countEnd-counter->count1Start)*8;
	tmp0 = value/tmp2;
	tmp1 = value%tmp2;
	count0Addr = (const uint32_t *)counter->count0Start+tmp0/32+MDATA_COUNTER_SIZE/4;
	count1Addr = (const uint32_t *)counter->count1Start+tmp1/32;
	value0 = 0xFFFFFFFF << tmp0%32;
	value1 = 0xFFFFFFFF << tmp1%32;
	tmp3 = *counter->count0Addr;

	if((count0Addr != counter->count0Addr) || (value0 != tmp3))
	{
		if(((count0Addr == counter->count0Addr) && (value0 > tmp3)) ||
			(count0Addr < counter->count0Addr))
		{
			NVMC_EraseSector((uint32_t *)counter->count0Start,
							 (counter->count1Start-counter->count0Start)
							 /SOC_FLASH_SECTOR_SIZE);
			NVMC_ProgramWord((uint32_t *)counter->count0Start,
			                 MDATA_COUNTER_FLAG);
			counter->count0Addr = (const uint32_t *)counter->count0Start
			                      +MDATA_COUNTER_SIZE/4;
		}

		for(addr = (uint32_t *)counter->count0Addr; addr < count0Addr; addr++)
		{
			NVMC_ProgramWord(addr, 0x00000000);
		}

		NVMC_ProgramWord(addr, value0);
	}

	tmp3 = *counter->count1Addr;

	if((count1Addr != counter->count1Addr) || (value1 != tmp3))
	{
		if(((count1Addr == counter->count1Addr) && (value1 > tmp3)) ||
			(count1Addr < counter->count1Addr))
		{
			NVMC_EraseSector((uint32_t *)counter->count1Start,
			                 (counter->countEnd-counter->count1Start)
			                 /SOC_FLASH_SECTOR_SIZE);
			counter->count1Addr = (const uint32_t *)counter->count1Start;
		}

		for(addr = (uint32_t *)counter->count1Addr; addr < count1Addr; addr++)
		{
			NVMC_ProgramWord(addr, 0x00000000);
		}

		NVMC_ProgramWord(addr, value1);
	}

	counter->count = value;

	return MDEV_STAT_OK;
}

MDevStat_e mDataCounterAddValue(MDataCounter_t *counter, uint32_t value)
{
	return mDataCounterSetValue(counter, value+counter->count);
}

MDevStat_e mDataCounterAddOne(MDataCounter_t *counter)
{
	uint32_t tmp1;

	counter->count++;
	tmp1 = *counter->count1Addr;
	tmp1 <<= 1;
	NVMC_ProgramWord((uint32_t *)counter->count1Addr, tmp1);

	if(tmp1 == 0x00000000)
	{
		counter->count1Addr++;

		if(counter->count1Addr == (const uint32_t *)counter->countEnd)
		{
			NVMC_EraseSector((uint32_t *)counter->count1Start,
			                 (counter->countEnd-counter->count1Start)
			                 /SOC_FLASH_SECTOR_SIZE);
			counter->count1Addr = (const uint32_t *)counter->count1Start;

			tmp1 = *counter->count0Addr;
			tmp1 <<= 1;
			NVMC_ProgramWord((uint32_t *)counter->count0Addr, tmp1);

			if(tmp1 == 0x00000000)
			{
				counter->count0Addr++;

				if(counter->count0Addr == (const uint32_t *)counter->count1Start)
				{
					NVMC_EraseSector((uint32_t *)counter->count0Start,
					                 (counter->count1Start-counter->count0Start)
					                 /SOC_FLASH_SECTOR_SIZE);
					NVMC_ProgramWord((uint32_t *)counter->count0Start,
					                 MDATA_COUNTER_FLAG);
					counter->count0Addr = (const uint32_t *)counter->count0Start
					                      +MDATA_COUNTER_SIZE/4;
					counter->count = 0;
				}
			}
		}
	}

	return MDEV_STAT_OK;
}

#include "mdebug.h"
#include "mgdb.h"
#define MDATA_COUNTER_TEST_START0    0x10008000
#define MDATA_COUNTER_TEST_START1    0x10008400
#define MDATA_COUNTER_TEST_END       0x10008800
static void mDataCounterTestPrint(const char *func)
{
	uint32_t i;

	mDebugPutStr("====== ");
	mDebugPutStr(func);
	mDebugPuts(" ======");

	for(i = 0; i < 5; i++)
	{
		mDebugPrintf("0x%08X: ", MDATA_COUNTER_TEST_START0+i*32);
		mDebugPutU32(((uint32_t *)MDATA_COUNTER_TEST_START0)+i*8, 8);
		mDebugLineFeed();
	}

	for(i = 0; i < 5; i++)
	{
		mDebugPrintf("0x%08X: ", MDATA_COUNTER_TEST_START1+i*32);
		mDebugPutU32(((uint32_t *)MDATA_COUNTER_TEST_START1)+i*8, 8);
		mDebugLineFeed();
	}
}

void mDataCounterTest(void)
{
	MDataCounter_t counter;

	mDataCounterInit(&counter, MDATA_COUNTER_TEST_START0,
	                 MDATA_COUNTER_TEST_START1, MDATA_COUNTER_TEST_END);
	mDebugPrintf("Current count: %d" MDEBUG_ENDL, counter.count);
	mDataCounterClear(&counter);
	mDataCounterTestPrint("clear");
	mDataCounterSetValue(&counter, 0);
	mDataCounterTestPrint("set 0");
	mDataCounterSetValue(&counter, 512);
	mDataCounterTestPrint("set 512");
	mDataCounterSetValue(&counter, 8192);
	mDataCounterTestPrint("set 8192");
	mDataCounterSetValue(&counter, 8193);
	mDataCounterTestPrint("set 8193");
	mDataCounterAddValue(&counter, 8193);
	mDataCounterTestPrint("add 8193");
	mDataCounterAddOne(&counter);
	mDataCounterTestPrint("add one");
	mDataCounterAddOne(&counter);
	mDataCounterTestPrint("add one");
}
#endif
